package com.yifeng.repo.base.service.impl;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.yifeng.base.mcs.api.MsgSendRestService;
import com.yifeng.base.mcs.api.dto.AppRoutingPolicyDto;
import com.yifeng.base.mcs.api.dto.AppTemplateContentDto;
import com.yifeng.base.mcs.api.dto.AppTemplateKeyDto;
import com.yifeng.base.mcs.api.params.SendMsgParam;
import com.yifeng.repo.base.params.ConsoleParam;
import com.yifeng.repo.base.security.proxy.CommonProxy;
import com.yifeng.repo.base.utils.converter.JacksonHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by daibing on 2024/2/3.
 */
public class MsgSendRestServiceImpl extends CommonProxy implements MsgSendRestService {
    private final String mapping;

    public MsgSendRestServiceImpl(ConsoleParam consoleParam) {
        super(consoleParam);
        this.mapping = "/yfcloud-mcs/rest/MsgSendRestService";
    }

    @Override
    public List<AppTemplateKeyDto> listTemplateKey(String appId) {
        String url = consoleParam.getUrl() + mapping + "/listTemplateKey";
        String queryParameters = buildSingleQueryParameters("appId", appId);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, AppTemplateKeyDto.class);
    }

    @Override
    public List<AppTemplateContentDto> listTemplateContent(String appId) {
        String url = consoleParam.getUrl() + mapping + "/listTemplateContent";
        String queryParameters = buildSingleQueryParameters("appId", appId);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, AppTemplateContentDto.class);
    }

    @Override
    public List<AppRoutingPolicyDto> listRoutingPolicy(String appId, String routingPolicyName) {
        String url = consoleParam.getUrl() + mapping + "/listRoutingPolicy";
        Map<String, String> params = new HashMap<>(2);
        params.put("appId", appId);
        params.put("routingPolicyName", routingPolicyName);
        String queryParameters = buildQueryParameters(params);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, AppRoutingPolicyDto.class);
    }

    @Override
    public AppRoutingPolicyDto getRoutingPolicyById(Long routingPolicyId) {
        String url = consoleParam.getUrl() + mapping + "/getRoutingPolicyById";
        String queryParameters = buildSingleQueryParameters("routingPolicyId", routingPolicyId);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doGetCallByRpc(requestUrl, AppRoutingPolicyDto.class);
    }

    @Override
    public List<AppTemplateKeyDto> listAppKeyByRoutingPolicy(Long routingPolicyId) {
        String url = consoleParam.getUrl() + mapping + "/listAppKeyByRoutingPolicy";
        String queryParameters = buildSingleQueryParameters("routingPolicyId", routingPolicyId);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, AppTemplateKeyDto.class);
    }

    @Override
    public String getSignByStoreCode(String storeCode) {
        String url = consoleParam.getUrl() + mapping + "/getSignByStoreCode";
        String queryParameters = buildSingleQueryParameters("storeCode", storeCode);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doGetCallByRpc(requestUrl, String.class);
    }

    @Override
    public Long sendCode(Long routingPolicyId, String phone, String storeCode, String code) {
        String url = consoleParam.getUrl() + mapping + "/sendCode";
        ObjectNode params = JacksonHelper.createEmptyJsonNode();
        params.put("routingPolicyId", routingPolicyId);
        params.put("phone", phone);
        params.put("storeCode", storeCode);
        params.put("code", code);
        return this.doActionCallByRpc(url, params.toString(), Long.class);
    }

    @Override
    public boolean checkCode(String appId, Long routingPolicyId, String phone, String code) {
        String url = consoleParam.getUrl() + mapping + "/checkCode";
        ObjectNode params = JacksonHelper.createEmptyJsonNode();
        params.put("appId", appId);
        params.put("routingPolicyId", routingPolicyId);
        params.put("phone", phone);
        params.put("code", code);
        return this.doActionCallByRpc(url, params.toString(), Boolean.class);
    }

    @Override
    public Long sendMsg(SendMsgParam param) {
        String url = consoleParam.getUrl() + mapping + "/sendMsg";
        String body = JacksonHelper.toJson(param);
        return this.doActionCallByRpc(url, body, Long.class);
    }

    @Override
    public void sendEmptyMsg() {
        throw new UnsupportedOperationException("业务调用方不需要！");
    }
}
